﻿#region Copyright (c) 2000-2013 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2013 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2013 Developer Express Inc.

using System;
using System.Windows.Controls;
using System.Windows;
using DevExpress.Xpf.Core;
using UColor.Core.Helpers;
using System.Windows.Input;
using UColor.WPF.Helpers;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Media;
namespace UColor.WPF.Internal {
	public sealed class BaseControlModuleWrapper : BaseControlOrdinarPart {
		WeakEventHandler<EventArgs> onDemoBaseControlModuleReload;
		WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>> onParentDemoFlowDirectionChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<BaseControlPage>> onDemoBaseControlStatePageChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlOptionsView>> onDemoBaseControlStateOptionsViewChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onDemoBaseControlStateOptionsIsExpandedChanged;
		WeakEventHandler<DepPropertyChangedEventArgs> onDemoBaseControlActualPageChanged;
		DemoModuleLoader demoModuleLoader;
		DemoModuleControlOptionsView optionsView = DemoModuleControlOptionsView.Options;
		DemoModuleControlView demoModuleView = DemoModuleControlView.Demo;
		FrameworkElement currentDemoModule;
        List<UCModule> ListModule = new List<UCModule>();
		bool hasOptions;
		bool allowThemes;
		Visibility sidebarVisibility = Visibility.Collapsed;
		string sidebarTag;
		ImageSource sidebarIcon;
		ImageSource sidebarIconSelected;
		LString shortDescription;
		LString description;
		LString title;
		bool allowRtl;
		FlowDirection demoFlowDirection;
		bool isDemoGone;
		bool? currentIsNextDemoRequested;
		bool? isNextDemoRequested;
		//ModuleDescription module;
		bool loadingInProgress = false;
		bool moduleLoadingInProgress = false;
		bool moduleChanged = false;
		//ModuleDescription loadModule;
		bool isOptionsExpanded = true;
		//ReadOnlyCollection<ReadOnlyCollection<ModuleLinkDescription>> links;
		bool showLinks = false;
		LoadingSplashBackground loadingBackground = LoadingSplashBackground.White;
		DefferableValue<string> csCodeText;
		DefferableValue<string> vbCodeText;
		DefferableValue<string> xamlText;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onCSCodeTextLoadingInProgressChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onVBCodeTextLoadingInProgressChanged;
		WeakEventHandler<ThePropertyChangedEventArgs<bool>> onXamlTextLoadingInProgressChanged;
		bool loadingInProgressCore = false;
		DemoModuleControlView nextView = DemoModuleControlView.Demo;
		public BaseControlModuleWrapper(BaseControlMainPage parent, BaseControl demoBaseControl)
			: base(demoBaseControl) {
			onCSCodeTextLoadingInProgressChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnCSCodeTextLoadingInProgressChanged);
			onVBCodeTextLoadingInProgressChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnVBCodeTextLoadingInProgressChanged);
			onXamlTextLoadingInProgressChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnXamlTextLoadingInProgressChanged);
			onDemoBaseControlModuleReload = new WeakEventHandler<EventArgs>(OnDemoBaseControlModuleReload);
			onDemoBaseControlStatePageChanged = new WeakEventHandler<ThePropertyChangedEventArgs<BaseControlPage>>(OnDemoBaseControlStatePageChanged);
			onDemoBaseControlStateOptionsViewChanged = new WeakEventHandler<ThePropertyChangedEventArgs<DemoModuleControlOptionsView>>(OnDemoBaseControlStateOptionsViewChanged);
			onDemoBaseControlStateOptionsIsExpandedChanged = new WeakEventHandler<ThePropertyChangedEventArgs<bool>>(OnDemoBaseControlStateOptionsIsExpandedChanged);
			onParentDemoFlowDirectionChanged = new WeakEventHandler<ThePropertyChangedEventArgs<FlowDirection>>(OnParentDemoFlowDirectionChanged);
			onDemoBaseControlActualPageChanged = new WeakEventHandler<DepPropertyChangedEventArgs>(OnDemoBaseControlActualPageChanged);
			parent.DemoFlowDirectionChanged += onParentDemoFlowDirectionChanged.Handler;
			OnParentDemoFlowDirectionChanged(parent, new ThePropertyChangedEventArgs<FlowDirection>(FlowDirection.LeftToRight, parent.DemoFlowDirection));
			ContentPresenter cp1 = (ContentPresenter)View.GetObjectFromVisualTree("DemoModule1Presenter");
			ContentPresenter cp2 = (ContentPresenter)View.GetObjectFromVisualTree("DemoModule2Presenter");
			Style dmcs = (Style)View.GetObjectFromResources("DemoModuleControl");
			demoModuleLoader = new DemoModuleLoader(cp1, cp2, dmcs);
			demoModuleLoader.CurrentDemoModuleHelper.Owner = this;
			demoModuleLoader.NextDemoModuleHelper.Owner = this;
			demoModuleLoader.NextDemoModuleLoaded += OnDemoModuleLoaderNextDemoModuleLoaded;
			DemoModuleView = DemoModuleControlView.Demo;
			AllowThemes = true;
			HasOptions = true;
			DemoBaseControl.ModuleReload += onDemoBaseControlModuleReload.Handler;
			DemoBaseControl.ActualPageChanged += onDemoBaseControlActualPageChanged.Handler;
            //demoModuleLoader.BeginLoadNextDemoModule(
#if !SL
			ThemeManager.ThemeChanged += new ThemeChangedRoutedEventHandler(OnThemeChanged);
#else
			ThemeManager.ActualApplicationThemeChanged += new ThemeChangedRoutedEventHandler(OnThemeChanged);
#endif
			Initialized();
		}
        public void LoadModule(Type moduleType)
        {
            demoModuleLoader.BeginLoadNextDemoModule(moduleType);
        }
        public void ClearModuleList()
        {
            ListModule.Clear();
        }
        public void ReplaceCurrentModuleByNext()
        {
            if (ListModule.Count > 0 && ListModule.Count > (currentIdx + 1))
            {
               currentIdx++;
               UCModule module = ListModule[currentIdx];
               demoModuleLoader.BeginLoadNextDemoModule(module);
               OnDemoGone();
               CurrentDemoModule = module;
               DemoBaseControl.State.Page = BaseControlPage.MainPage;
            }
        }
        public void ReplaceCurrentModuleByPrev()
        {
            if (ListModule.Count > 0 && ListModule.Count > (currentIdx - 1) && (currentIdx - 1) >= 0)
            {
                currentIdx--;
                UCModule module = ListModule[currentIdx];
                demoModuleLoader.BeginLoadNextDemoModule(module);
                OnDemoGone();
                CurrentDemoModule = module;
                DemoBaseControl.State.Page = BaseControlPage.MainPage;
            }
        }
        private int currentIdx = 0;
        public void LoadModule(UCModule module)
        {
            demoModuleLoader.BeginLoadNextDemoModule(module);
            CurrentDemoModule = module;
            ListModule.Add(module);
            currentIdx = ListModule.Count - 1;
            DemoBaseControl.State.Page = BaseControlPage.MainPage;
        }
		public DefferableValue<string> CSCodeText {
			get { return csCodeText; }
			private set { SetValue<DefferableValue<string>>("CSCodeText", ref csCodeText, value, RaiseCSCodeTextChanged); }
		}
		public DefferableValue<string> VBCodeText {
			get { return vbCodeText; }
			private set { SetValue<DefferableValue<string>>("VBCodeText", ref vbCodeText, value, RaiseVBCodeTextChanged); }
		}
		public DefferableValue<string> XamlText {
			get { return xamlText; }
			private set { SetValue<DefferableValue<string>>("XamlText", ref xamlText, value, RaiseXamlTextChanged); }
		}
		public bool IsDemoGone {
			get { return isDemoGone; }
			private set { SetValue<bool>("IsDemoGone", ref isDemoGone, value); }
		}
		public bool? IsNextDemoRequested {
			get { return isNextDemoRequested; }
			set { SetValue<bool?>("IsNextDemoRequested", ref isNextDemoRequested, value); }
		}
		public bool? CurrentIsNextDemoRequested {
			get { return currentIsNextDemoRequested; }
			private set { SetValue<bool?>("CurrentIsNextDemoRequested", ref currentIsNextDemoRequested, value); }
		}
		public DemoModuleControlOptionsView OptionsView {
			get { return optionsView; }
			set { SetValue<DemoModuleControlOptionsView>("OptionsView", ref optionsView, value, RaiseOptionsViewChanged); }
		}
		public DemoModuleControlView DemoModuleView {
			get { return demoModuleView; }
			set { SetValue<DemoModuleControlView>("DemoModuleView", ref demoModuleView, value, OnDemoModuleViewChanged); }
		}
		public FrameworkElement CurrentDemoModule {
			get { return currentDemoModule; }
			private set { SetValue<FrameworkElement>("CurrentDemoModule", ref currentDemoModule, value); }
		}
		public bool HasOptions {
			get { return hasOptions; }
			private set { SetValue<bool>("HasOptions", ref hasOptions, value, RaiseHasOptionsChanged); }
		}
		public bool AllowThemes {
			get { return allowThemes; }
			private set { SetValue<bool>("AllowThemes", ref allowThemes, value, RaiseAllowThemesChanged); }
		}
		public LString Title {
			get { return title; }
			private set { SetValue<LString>("Title", ref title, value, RaiseTitleChanged); }
		}
		public bool AllowRtl {
			get { return allowRtl; }
			private set { SetValue<bool>("AllowRtl", ref allowRtl, value, RaiseAllowRtlChanged); }
		}
		public Visibility SidebarVisibility {
			get { return sidebarVisibility; }
			private set { SetValue<Visibility>("SidebarVisibility", ref sidebarVisibility, value, RaiseSidebarVisibilityChanged); }
		}
		public string SidebarTag {
			get { return sidebarTag; }
			private set { SetValue<string>("SidebarTag", ref sidebarTag, value, RaiseSidebarTagChanged); }
		}
		public ImageSource SidebarIcon {
			get { return sidebarIcon; }
			private set { SetValue<ImageSource>("SidebarIcon", ref sidebarIcon, value, RaiseSidebarIconChanged); }
		}
		public ImageSource SidebarIconSelected {
			get { return sidebarIconSelected; }
			private set { SetValue<ImageSource>("SidebarIconSelected", ref sidebarIconSelected, value, RaiseSidebarIconSelectedChanged); }
		}
		public LString ShortDescription {
			get { return shortDescription; }
			private set { SetValue<LString>("ShortDescription", ref shortDescription, value); }
		}
		public LString Description {
			get { return description; }
			private set { SetValue<LString>("Description", ref description, value); }
		}
		public FlowDirection DemoFlowDirection {
			get { return demoFlowDirection; }
			set { SetValue<FlowDirection>("DemoFlowDirection", ref demoFlowDirection, value, RaiseDemoFlowDirectionChanged); }
		}
		
		public bool LoadingInProgress {
			get { return loadingInProgress; }
			private set { SetValue<bool>("LoadingInProgress", ref loadingInProgress, value, RaiseLoadingInProgressChanged); }
		}
		public bool IsOptionsExpanded {
			get { return isOptionsExpanded; }
			set { SetValue<bool>("IsOptionsExpanded", ref isOptionsExpanded, value, RaiseIsOptionsExpandedChanged); }
		}
		
		public bool ShowLinks {
			get { return showLinks; }
			set { SetValue<bool>("ShowLinks", ref showLinks, value); }
		}
		public LoadingSplashBackground LoadingBackground {
			get { return loadingBackground; }
			private set { SetValue<LoadingSplashBackground>("LoadingBackground", ref loadingBackground, value); }
		}
		public event EventHandler<ModuleAppearEventArgs> ModuleAppear;
		public event ThePropertyChangedEventHandler<LString> TitleChanged;
		public event ThePropertyChangedEventHandler<DemoModuleControlView> DemoModuleViewChanged;
		public event ThePropertyChangedEventHandler<DemoModuleControlOptionsView> OptionsViewChanged;
		public event ThePropertyChangedEventHandler<FlowDirection> DemoFlowDirectionChanged;
		public event ThePropertyChangedEventHandler<bool> IsOptionsExpandedChanged;
		public event ThePropertyChangedEventHandler<bool> AllowThemesChanged;
		public event ThePropertyChangedEventHandler<bool> HasOptionsChanged;
		public event ThePropertyChangedEventHandler<bool> LoadingInProgressChanged;
		public event ThePropertyChangedEventHandler<bool> AllowRtlChanged;
		public event ThePropertyChangedEventHandler<Visibility> SidebarVisibilityChanged;
		public event ThePropertyChangedEventHandler<string> SidebarTagChanged;
		public event ThePropertyChangedEventHandler<ImageSource> SidebarIconChanged;
		public event ThePropertyChangedEventHandler<ImageSource> SidebarIconSelectedChanged;
		public bool CanLeave() {
			return this.demoModuleLoader == null || this.demoModuleLoader.CurrentDemoModuleHelper.CanLeave();
		}
		protected override void OnDemoBaseControlStateChanged(BaseControlState oldValue, BaseControlState newValue) {
			base.OnDemoBaseControlStateChanged(oldValue, newValue);
			if(oldValue != null) {
				oldValue.PageChanged -= onDemoBaseControlStatePageChanged.Handler;
				oldValue.OptionsViewChanged -= onDemoBaseControlStateOptionsViewChanged.Handler;
				oldValue.OptionsIsExpandedChanged -= onDemoBaseControlStateOptionsIsExpandedChanged.Handler;
			}
			if(newValue != null) {
				newValue.PageChanged += onDemoBaseControlStatePageChanged.Handler;
				OnDemoBaseControlStatePageChanged(newValue, new ThePropertyChangedEventArgs<BaseControlPage>(BaseControlPage.None, newValue.Page));
				newValue.OptionsViewChanged += onDemoBaseControlStateOptionsViewChanged.Handler;
				OnDemoBaseControlStateOptionsViewChanged(newValue, new ThePropertyChangedEventArgs<DemoModuleControlOptionsView>(DemoModuleControlOptionsView.Options, newValue.OptionsView));
				newValue.OptionsIsExpandedChanged += onDemoBaseControlStateOptionsIsExpandedChanged.Handler;
				OnDemoBaseControlStateOptionsIsExpandedChanged(newValue, new ThePropertyChangedEventArgs<bool>(false, newValue.OptionsIsExpanded));
			}
		}
		void RaiseCSCodeTextChanged(DefferableValue<string> oldValue, DefferableValue<string> newValue) {
			if(oldValue != null)
				oldValue.LoadingInProgressChanged -= onCSCodeTextLoadingInProgressChanged.Handler;
			if(newValue != null)
				newValue.LoadingInProgressChanged += onCSCodeTextLoadingInProgressChanged.Handler;
		}
		void RaiseVBCodeTextChanged(DefferableValue<string> oldValue, DefferableValue<string> newValue) {
			if(oldValue != null)
				oldValue.LoadingInProgressChanged -= onVBCodeTextLoadingInProgressChanged.Handler;
			if(newValue != null)
				newValue.LoadingInProgressChanged += onVBCodeTextLoadingInProgressChanged.Handler;
		}
		void RaiseXamlTextChanged(DefferableValue<string> oldValue, DefferableValue<string> newValue) {
			if(oldValue != null)
				oldValue.LoadingInProgressChanged -= onXamlTextLoadingInProgressChanged.Handler;
			if(newValue != null)
				newValue.LoadingInProgressChanged += onXamlTextLoadingInProgressChanged.Handler;
		}
		void OnCSCodeTextLoadingInProgressChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			UpdateLoadingInProgress();
		}
		void OnVBCodeTextLoadingInProgressChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			UpdateLoadingInProgress();
		}
		void OnXamlTextLoadingInProgressChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			UpdateLoadingInProgress();
		}
		void UpdateLoadingInProgress() {
			LoadingInProgress = this.loadingInProgressCore || CSCodeText != null && CSCodeText.LoadingInProgress || VBCodeText != null && VBCodeText.LoadingInProgress || XamlText != null && XamlText.LoadingInProgress;
		}
		void OnThemeChanged(DependencyObject sender, ThemeChangedRoutedEventArgs e) {
			if(e.ThemeName != "Demo" && demoModuleView == DemoModuleControlView.Demo)
				LoadingBackground = ThemeSelectorHelper.ThemeToLoadingSplashBackground(e.ThemeName);
		}
		void OnDemoBaseControlStatePageChanged(object sender, ThePropertyChangedEventArgs<BaseControlPage> e) {
			bool mustBeInvisible = e.NewValue != BaseControlPage.MainPage;
			if(mustBeInvisible)
				this.demoModuleLoader.IsPopupContentInvisible = true;
		}
		void OnDemoBaseControlActualPageChanged(object sender, DepPropertyChangedEventArgs e) {
			bool mustBeInvisible = (BaseControlPage)e.NewValue != BaseControlPage.MainPage;
			if(!mustBeInvisible)
				this.demoModuleLoader.IsPopupContentInvisible = false;
		}
		void OnDemoBaseControlStateOptionsViewChanged(object sender, ThePropertyChangedEventArgs<DemoModuleControlOptionsView> e) {
			OptionsView = e.NewValue;
		}
		void OnDemoBaseControlStateOptionsIsExpandedChanged(object sender, ThePropertyChangedEventArgs<bool> e) {
			IsOptionsExpanded = e.NewValue;
		}
		void BeginLoadNextModule() {
			if(this.moduleLoadingInProgress || !this.moduleChanged) return;
			this.loadingInProgressCore = true;
			UpdateLoadingInProgress();
			this.moduleLoadingInProgress = true;
			this.moduleChanged = false;
			
			CurrentIsNextDemoRequested = IsNextDemoRequested;
			IsNextDemoRequested = null;
			//this.demoModuleLoader.BeginLoadNextDemoModule(this.loadModule == null ? null : this.loadModule.ModuleType);
		}
		void OnDemoModuleLoaderNextDemoModuleLoaded(object sender, EventArgs e) {
			DoInBackgroundThread(null, () => {
				IsDemoGone = true;
				this.loadingInProgressCore = false;
				UpdateLoadingInProgress();
			});
		}
		public void OnDemoGone() {
			//ReplaceCurrentDemoModuleByNext();
			DoInBackgroundThread(null, () => {
				IsDemoGone = false;
			});
		}
        public void ReplaceCurrentDemoModuleByNext()
        {
            demoModuleLoader.CurrentDemoModuleHelper.ClearValue(ModuleHelper.DemoModuleViewProperty);
            demoModuleLoader.CurrentDemoModuleHelper.ClearValue(ModuleHelper.DemoModuleOptionsViewProperty);
            demoModuleLoader.CurrentDemoModuleHelper.ClearValue(ModuleHelper.OptionsExpandedProperty);
            //CSCodeText = this.loadModule == null ? null : this.loadModule.CSCodeText;
            //VBCodeText = this.loadModule == null ? null : this.loadModule.VBCodeText;
            //XamlText = this.loadModule == null ? null : this.loadModule.XamlText;
            UpdateLoadingInProgress();
            this.demoModuleLoader.ReplaceCurrentDemoModuleByNext();
            //ShortDescription = this.loadModule == null ? LString.Empty : this.loadModule.ShortDescription;
            //Description = this.loadModule == null ? LString.Empty : this.loadModule.Description;
            //Title = this.loadModule == null ? LString.Empty : this.loadModule.Title;
            //AllowRtl = this.loadModule == null ? true : this.loadModule.AllowRtl;
            //SidebarVisibility = this.loadModule == null ? Visibility.Collapsed : demoModuleLoader.CurrentDemoModuleHelper.SidebarVisibility;
            //SidebarTag = this.loadModule == null ? "Sidebar" : demoModuleLoader.CurrentDemoModuleHelper.SidebarTag;
            //SidebarIcon = this.loadModule == null ? null : demoModuleLoader.CurrentDemoModuleHelper.SidebarIcon;
            //SidebarIconSelected = this.loadModule == null ? null : demoModuleLoader.CurrentDemoModuleHelper.SidebarIconSelected;
            HasOptions = demoModuleLoader.CurrentDemoModuleHelper.HasOptionsContent;
            DemoModuleView = demoModuleLoader.CurrentDemoModuleHelper.DemoModuleView;
            BindingOperations.SetBinding(demoModuleLoader.CurrentDemoModuleHelper, ModuleHelper.DemoModuleViewProperty, new Binding("DemoModuleView") { Source = this, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
            OptionsView = demoModuleLoader.CurrentDemoModuleHelper.DemoModuleOptionsView;
            BindingOperations.SetBinding(demoModuleLoader.CurrentDemoModuleHelper, ModuleHelper.DemoModuleOptionsViewProperty, new Binding("OptionsView") { Source = this, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
            IsOptionsExpanded = demoModuleLoader.CurrentDemoModuleHelper.OptionsExpanded;
            BindingOperations.SetBinding(demoModuleLoader.CurrentDemoModuleHelper, ModuleHelper.OptionsExpandedProperty, new Binding("IsOptionsExpanded") { Source = this, Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
           // Links = this.loadModule == null ? null : this.loadModule.Links;
            CurrentDemoModule = demoModuleLoader.CurrentDemoModuleHelper.DemoModule;
            AllowThemes = demoModuleLoader.CurrentDemoModuleHelper.AllowThemeChange;
            UpdateDemoFlowDirection(DemoFlowDirection);
        }
		public void OnDemoCame() {
			DemoBaseControl.ActualPageChanged += OnDemoBaseControlActualPageChanged2;
			if(DemoBaseControl.ActualPage == BaseControlPage.MainPage)
				OnDemoBaseControlActualPageChanged2(DemoBaseControl, null);
			DemoBaseControl.State.Page = BaseControlPage.MainPage;
		}
		void OnDemoBaseControlActualPageChanged2(object sender, DepPropertyChangedEventArgs e) {
			if(DemoBaseControl.ActualPage != BaseControlPage.MainPage) return;
			DemoBaseControl.ActualPageChanged -= OnDemoBaseControlActualPageChanged2;
			this.demoModuleLoader.AppearCurrentDemoModule();
			if(ModuleAppear != null)
				ModuleAppear(this, new ModuleAppearEventArgs(this.demoModuleLoader.CurrentDemoModuleHelper.DemoModule, this.demoModuleLoader.CurrentDemoModuleHelper.DemoModuleException));
			this.moduleLoadingInProgress = false;
			BeginLoadNextModule();
		}
		void RaiseLoadingInProgressChanged(bool oldValue, bool newValue) {
			if(LoadingInProgressChanged != null) {
				LoadingInProgressChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
			}
		}
        //void RaiseModuleChanged(ModuleDescription oldValue, ModuleDescription newValue) {
        //    this.moduleChanged = true;
        //    BeginLoadNextModule();
        //}
		void UpdateDemoFlowDirection(FlowDirection demoFlowDirection) {
			this.demoModuleLoader.CurrentDemoModuleHelper.DemoFlowDirection = demoFlowDirection;
		}
		void OnDemoModuleViewChanged(DemoModuleControlView oldValue, DemoModuleControlView newValue) {
			switch(newValue) {
				case DemoModuleControlView.CSCode: LoadCSCode(oldValue); break;
				case DemoModuleControlView.VBCode: LoadVBCode(oldValue); break;
				case DemoModuleControlView.Xaml: LoadXaml(oldValue); break;
				default: LoadDemo(oldValue); break;
			}
		}
		void LoadCSCode(DemoModuleControlView oldValue) {
			if(CSCodeText != null)
				CSCodeText.Load();
			this.nextView = DemoModuleControlView.CSCode;
			ChangeCurrentModuleViewAndRaiseChanged(oldValue);
		}
		void LoadVBCode(DemoModuleControlView oldValue) {
			if(VBCodeText != null)
				VBCodeText.Load();
			this.nextView = DemoModuleControlView.VBCode;
			ChangeCurrentModuleViewAndRaiseChanged(oldValue);
		}
		void LoadXaml(DemoModuleControlView oldValue) {
			if(XamlText != null)
				XamlText.Load();
			this.nextView = DemoModuleControlView.Xaml;
			ChangeCurrentModuleViewAndRaiseChanged(oldValue);
		}
		void LoadDemo(DemoModuleControlView oldValue) {
			this.nextView = DemoModuleControlView.Demo;
			ChangeCurrentModuleViewAndRaiseChanged(oldValue);
		}
		void ChangeCurrentModuleViewAndRaiseChanged(DemoModuleControlView oldView) {
			if(LoadingInProgress || oldView == nextView) return;
			this.demoModuleLoader.CurrentDemoModuleHelper.DemoModuleView = nextView;
			if(nextView == DemoModuleControlView.Demo)
				LoadingBackground = ThemeSelectorHelper.ThemeToLoadingSplashBackground(ThemeManager.ApplicationThemeName);
			else
				LoadingBackground = LoadingSplashBackground.White;
			if(DemoModuleViewChanged != null)
				DemoModuleViewChanged(this, new ThePropertyChangedEventArgs<DemoModuleControlView>(oldView, nextView));
		}
		void RaiseDemoFlowDirectionChanged(FlowDirection oldValue, FlowDirection newValue) {
			demoModuleLoader.CurrentDemoModuleHelper.DemoFlowDirection = newValue;
			if(DemoFlowDirectionChanged != null)
				DemoFlowDirectionChanged(this, new ThePropertyChangedEventArgs<FlowDirection>(oldValue, newValue));
		}
		void RaiseOptionsViewChanged(DemoModuleControlOptionsView oldValue, DemoModuleControlOptionsView newValue) {
			if(DemoBaseControl.State == null) return;
			DemoBaseControl.State.OptionsView = newValue;
			if(OptionsViewChanged != null)
				OptionsViewChanged(this, new ThePropertyChangedEventArgs<DemoModuleControlOptionsView>(oldValue, newValue));
		}
		void RaiseTitleChanged(LString oldValue, LString newValue) {
			if(TitleChanged != null)
				TitleChanged(this, new ThePropertyChangedEventArgs<LString>(oldValue, newValue));
		}
		void RaiseIsOptionsExpandedChanged(bool oldValue, bool newValue) {
			if(IsOptionsExpandedChanged != null)
				IsOptionsExpandedChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
			if(DemoBaseControl.State == null) return;
			DemoBaseControl.State.OptionsIsExpanded = newValue;
		}
		void RaiseAllowThemesChanged(bool oldValue, bool newValue) {
			if(AllowThemesChanged != null)
				AllowThemesChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
		}
		void RaiseHasOptionsChanged(bool oldValue, bool newValue) {
			if(HasOptionsChanged != null)
				HasOptionsChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
		}
        //void RaiseLinksChanged(ReadOnlyCollection<ReadOnlyCollection<ModuleLinkDescription>> oldValue, ReadOnlyCollection<ReadOnlyCollection<ModuleLinkDescription>> newValue) {
        //    ShowLinks = newValue != null && newValue.Count != 0;
        //}
		void RaiseAllowRtlChanged(bool oldValue, bool newValue) {
			if(AllowRtlChanged != null) {
				AllowRtlChanged(this, new ThePropertyChangedEventArgs<bool>(oldValue, newValue));
			}
		}
		void RaiseSidebarVisibilityChanged(Visibility oldValue, Visibility newValue) {
			if(SidebarVisibilityChanged != null)
				SidebarVisibilityChanged(this, new ThePropertyChangedEventArgs<Visibility>(oldValue, newValue));
		}
		void RaiseSidebarTagChanged(string oldValue, string newValue) {
			if(SidebarTagChanged != null)
				SidebarTagChanged(this, new ThePropertyChangedEventArgs<string>(oldValue, newValue));
		}
		void RaiseSidebarIconChanged(ImageSource oldValue, ImageSource newValue) {
			if(SidebarIconChanged != null)
				SidebarIconChanged(this, new ThePropertyChangedEventArgs<ImageSource>(oldValue, newValue));
		}
		void RaiseSidebarIconSelectedChanged(ImageSource oldValue, ImageSource newValue) {
			if(SidebarIconSelectedChanged != null)
				SidebarIconSelectedChanged(this, new ThePropertyChangedEventArgs<ImageSource>(oldValue, newValue));
		}
		void OnDemoBaseControlModuleReload(object sender, EventArgs e) {
			this.moduleChanged = true;
			BeginLoadNextModule();
		}
		void OnParentDemoFlowDirectionChanged(object sender, ThePropertyChangedEventArgs<FlowDirection> e) {
			DemoFlowDirection = e.NewValue;
		}
		#region Commands
		protected override void InitializeCommands() {
			base.InitializeCommands();
			OnDemoGoneCommand = new SimpleActionCommand(DoOnDemoGone);
			OnDemoCameCommand = new SimpleActionCommand(DoOnDemoCame);
		}
		public ICommand OnDemoGoneCommand { get; private set; }
		void DoOnDemoGone(object p) { OnDemoGone(); }
		public ICommand OnDemoCameCommand { get; private set; }
		void DoOnDemoCame(object p) { OnDemoCame(); }
		#endregion
	}
}
